package com.example.mydemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mydemo.entity.User;
import com.example.mydemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
//    @GetMapping("/user")
//    public String user() {
//        return "user";
//    }
    @GetMapping("/user/{id}")
    public String getUserById(@PathVariable int id){
        System.out.println(id);
        return "查找特定用户";
    }
    @DeleteMapping("/user/{id}")
    public String delUserById(@PathVariable int id) {
        System.out.println(id);
        return "删除特定id用户";
    }

    @Autowired
    private UserMapper userMapper;
    @GetMapping("/user")
//    public String query(){
//        List<User> list = userMapper.find();
//        System.out.println(list);
//        return "查询用户";
//    }
    public List query() {
        List<User> list = userMapper.selectList(null);
        System.out.println(list);
        return list;
    }

    @PostMapping("/user")
    public String save(User user){
        int i = userMapper.insert(user);
        if(i > 0){
            return "插入成功";
        }else{
            return "插入失败";
        }
    }

//    多表查询
    @GetMapping("/user/findAll")
    public List<User> find(){
        return userMapper.selectAllUserAndOrder();
    }

    //  条件查询
    @GetMapping("/user/find")
    public List<User> findByCond(){
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("name","mr.l");
        return userMapper.selectList(queryWrapper);
    }

    //分页查询
    @GetMapping("/user/findByPage")
    public IPage findByPage(){
        //设置起始值及每页条数
        Page<User> page = new Page<>(0,2);

        IPage iPage = userMapper.selectPage(page,null);
        return iPage;
    }


}
